package com.aco.party.web.controller.client;

import com.aco.party.dao.custom.PostsDao;
import com.aco.party.enums.PostTypeEnum;
import com.aco.party.enums.PostsStatusEnum;
import com.aco.party.model.po.OldPosts;
import com.aco.party.model.po.OldPostsImg;
import com.aco.party.model.po.OldTerms;
import com.aco.party.model.po.Posts;
import com.aco.party.util.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by WangShijun on 2016/4/23.
 */
@Controller
public class TransDataController {
    private Logger logger = Logger.getLogger(getClass().getName());
    @RequestMapping("/trans")
    public String trans(){
        int i = 0;
        List<Long> terms = Arrays.asList(172L, 171L, 169L, 162L, 164L);
        PostsDao postsDao = (PostsDao) SpringContextUtil.getBean("postsDao");
        Posts posts;
        if(postsDao != null){
            List<OldPosts> oldPostsList = postsDao.getOldPosts();
            if(oldPostsList != null){
                for (OldPosts oldPosts :oldPostsList){
                    posts = new Posts();
                    List<OldTerms> oldTermses = postsDao.getOldTerms(oldPosts.getID());
                    logger.info(String.format("postId:%s,oldTerms",oldPosts.getID(), StringUtils.join(oldTermses,",")));
                    if(oldTermses != null){
                        for (OldTerms oldTerms : oldTermses){
                            if(posts.getPostTerm() == null) {
                                for (Long term : terms) {
                                    if (term.intValue() == oldTerms.getTerm_taxonomy_id().intValue()-1) {
                                        if(term == 172) continue;
                                        posts.setPostTerm(term);
                                        break;
                                    }
                                }
                            }else{
                                break;
                            }
                        }
                        if(posts.getPostTerm() == null){
                            posts.setPostTerm(172L);
                        }
                    }
                    logger.info(posts.getPostTerm()+"");
                    posts.setPostStatus(PostsStatusEnum.显示.getValue());
                    posts.setPostType(PostTypeEnum.普通.getValue());
                    posts.setCreateTime(oldPosts.getPost_date());
                    posts.setUpdateTime(oldPosts.getPost_modified());
                    posts.setPostAuthor(1L);
                    posts.setPostTitle(oldPosts.getPost_title());
                    OldPostsImg oldPostsImg = postsDao.getOldPostsImg(oldPosts.getID());
                    if(oldPostsImg != null){
                        posts.setPostImg(oldPostsImg.getGuid());
                    }else{
                        posts.setPostImg("posts_默认图001.png");
                    }
                    if(!StringUtils.isEmpty(oldPosts.getPost_content())) {
                        posts.setPostContent(oldPosts.getPost_content().replace("http://morketing.cn/wp-content/uploads/", "${base}/system/getImg?imgId="));
                    }else{
                        posts.setPostContent("");
                    }
                    postsDao.insertSelective(posts);
                    i++;
                }
            }
        }
        return i + "";
    }

    public static void main(String[] args) {
        Posts posts = new Posts();
        List<Long> terms = Arrays.asList(172L, 171L, 169L, 162L, 164L);
        List<OldTerms> oldTermses = Arrays.asList(new OldTerms(170L),new OldTerms(173L),new OldTerms(165L));
        if(oldTermses != null){
            for (OldTerms oldTerms : oldTermses){
                if(posts.getPostTerm() == null) {
                    for (Long term : terms) {
                        if (term.intValue() == oldTerms.getTerm_taxonomy_id().intValue()-1) {
                            if(term == 172) continue;
                            posts.setPostTerm(term);
                            break;
                        }
                    }
                }else{
                    break;
                }
            }
            if(posts.getPostTerm() == null){
                posts.setPostTerm(172L);
            }
            System.out.println(posts.getPostTerm());
        }
    }
}
